package graphdao

import (
	"power/core/common/daoutil"
	"power/core/dao/table"
	"power/core/service/common"

	"strconv"

	"github.com/RocksonZeta/wrap/redised"
	"github.com/RocksonZeta/wrap/utils/cutil"
	"github.com/RocksonZeta/wrap/utils/sutil"
	"github.com/RocksonZeta/wrap/utils/timeutil"
)

const tableGraphElementProtoClass = "GraphElementProtoClass"

type GraphElementProtoClasss struct {
	*redised.RedisedMysql
}

func (s GraphElementProtoClasss) redisKey(id int) string {
	return tableGraphElementProtoClass + "/" + strconv.Itoa(id)
}

// func (s GraphElementProtoClasss) redisKeyAll() string {
// 	return tableGraphElementProtoClass + "/all"
// }

func (s GraphElementProtoClasss) ClearCache(old table.GraphElementProtoClass) {
	s.Redis.Del(s.redisKey(old.Id))
	// s.Redis.Del(s.redisKeyAll())
	s.Redis.Del(s.KVFn(tableGraphElementProtoClass, sutil.M{"uid": old.Uid}))
}

type GraphElementProtoClasssQueryParam struct {
	common.Page
	Name string
}

//Query 根据条件查询
func (s GraphElementProtoClasss) Query(param GraphElementProtoClasssQueryParam) ([]table.GraphElementProtoClass, int64) {
	wsql := " from GraphElementProtoClass where 1=1"
	if param.Name != "" {
		param.Name = "%" + param.Name + "%"
		wsql += " and name like :Name"
	}
	ssql := "select *" + wsql + " order by ct desc" + param.Limit()
	csql := "select count(*)" + wsql
	var r []table.GraphElementProtoClass
	s.Mysql.Select(&r, ssql, param)
	return r, s.Mysql.SelectInt(csql, param)
}

func (s GraphElementProtoClasss) Get(id int) table.GraphElementProtoClass {
	var r table.GraphElementProtoClass
	s.RedisedMysql.Get(&r, tableGraphElementProtoClass, "Id", id)
	return r
}

func (s GraphElementProtoClasss) List(ids []int) []table.GraphElementProtoClass {
	var r []table.GraphElementProtoClass
	s.RedisedMysql.List(&r, tableGraphElementProtoClass, "Id", ids)
	return r
}

// func (s GraphElementProtoClasss) ListAll() []table.GraphElementProtoClass {
// 	var r []table.GraphElementProtoClass
// 	s.RedisedMysql.ListBySql(&r, s.redisKeyAll(), "select * from "+tableGraphElementProtoClass+" order by no")
// 	return r
// }

func (s GraphElementProtoClasss) ListByUid(uid int) []table.GraphElementProtoClass {
	var r []table.GraphElementProtoClass
	s.RedisedMysql.ListByKvs(&r, tableGraphElementProtoClass, "Id", sutil.M{"uid": uid})
	cutil.Sort(r, "No", true)
	return r
}

func (s GraphElementProtoClasss) CountByUid(uid int) int {
	r := s.RedisedMysql.Mysql.SelectInt("select count(*) from "+tableGraphElementProtoClass+" where uid=:uid", sutil.M{"uid": uid})
	return int(r)
}

type GraphElementProtoClasssAddParam struct {
	Name string
	Uid  int
}

func (s GraphElementProtoClasss) Add(param GraphElementProtoClasssAddParam) table.GraphElementProtoClass {
	var t table.GraphElementProtoClass
	sutil.Copy(param, &t)
	t.Ct = timeutil.Now()
	t.No = s.CountByUid(param.Uid) + 1
	s.Mysql.AddTable(t)
	s.Mysql.Insert(&t)
	s.ClearCache(t)
	return t
}

type GraphElementProtoClasssUpdateParam struct {
	Id   int
	Name string
	No   int
}

func (s GraphElementProtoClasss) Update(param GraphElementProtoClasssUpdateParam) {
	s.Mysql.Patch(tableGraphElementProtoClass, "Id", param)
	old := s.Get(param.Id)
	s.updateNo(old.Uid, param.Id, param.No)
	s.ClearCache(old)
}
func (s GraphElementProtoClasss) updateNo(uid, id, no int) {
	list := s.ListByUid(uid)
	sqls := daoutil.SetNoSqls(list, id, no, "Id", "No", tableGraphElementProtoClass)
	for _, v := range sqls {
		s.Mysql.Exec(v)
	}
}

func (s GraphElementProtoClasss) Delete(id int) {
	old := s.Get(id)
	s.Mysql.Delete(tableGraphElementProtoClass, "Id", id)
	s.ClearCache(old)

}
